import {reqAddOrUpdateShopCart, reqGoodsInfo} from "@/api";
//封装游客身份模块的uuid-->生成一个随机字符串(存在的话就不能变了)
import {getUUID} from '@/utils/uuid_token'
const state = {
    goodInfo: {},
    //游客临时身份
    uuid_token: getUUID(),
}
const mutations = {
    GET_GOOD_INFO(state, goodInfo) {
        state.goodInfo = goodInfo
    },
}
const actions = {
    async getGoodInfo({commit}, skuId) {
        let request = await reqGoodsInfo(skuId)
        if (request.code === 200) {
            commit('GET_GOOD_INFO', request.data)
        }
    },

    async addOrderUpdateShopCart({commit}, {skuId, skuNum}) {
        //加入购物车返回的结果
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        if (result.code === 200) {
            //加入购物车成功
            return 'ok';
        } else {
            return Promise.reject(new Error('fail'))
        }
    },
}
//简化数据而和一
const getters = {
    //路径导航简化的数据
    categoryView(state) {
        return state.goodInfo.categoryView || {}
    },
    //简化产品信息的数据
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    //产品售卖属性的简化

    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || []
    },

}
export default {
    state,
    mutations,
    actions,
    getters
}